import 'dart:async';

import 'package:elmer_tools/common/Utils.dart';
import 'package:elmer_tools/pages/welcome/Configuration.dart';
import 'package:elmer_tools/pages/welcome/WCBackground.dart';
import 'package:flutter/material.dart';
import 'dart:ui' as ui;

class Welcome extends StatefulWidget {
    const Welcome({super.key});
    
    @override
    State<Welcome> createState() => WelcomeState();
}

class WelcomeState extends State<Welcome> {
    ui.Image? image1;
    ui.Image? image2;
    ui.Image? image3;
    ui.Image? image4;

    Timer? timer;
    int timeCount = 5;
    bool isTick = true;
    int imageIndex = 1;
    BuildContext? currentContext;

    WelcomeState(): imageIndex = initActiveIndex();

    static int initActiveIndex() {
        DateTime now = DateTime.now();
        int activeIndex = now.weekday % 4;
        return activeIndex;
    }

    Widget initLoading(double width) {
        return Container(
            width: 60,
            height: 60,
            margin: EdgeInsets.fromLTRB((width - 60) / 2, 200, (width - 60) / 2, 0)
        );
    }

    @override
    Widget build(BuildContext context) {
        double sizeWidth = Utils.getWidth(context);
        double sizeHeight = Utils.getHeight(context);
        currentContext = context;
        initActiveIndex();
        bool isLoadImage = (image1 == null || image2 == null || image3 == null || image4 == null);
        print("welcome build: $sizeWidth, $sizeHeight, activeIndex: $imageIndex, loading: $isLoadImage");
        return (image1 == null || image2 == null || image3 == null || image4 == null) ? initLoading(sizeWidth) : Container(
            width: sizeWidth,
            height: sizeHeight,
            decoration: const BoxDecoration(
                color: Color.fromARGB(255, 17, 28, 54)
            ),
            child: CustomPaint(
                size: Size(sizeWidth, sizeHeight),
                painter: WCBackground(
                    image1: image1!,
                    image2: image2!,
                    image3: image3!,
                    image4: image4!,
                    time: timeCount,
                    isTick: isTick,
                    activeIndex: imageIndex
                ),
                child: Configuration(),
            )
        );
    }

    @override
    void initState() {
        super.initState();
        loadImage();
        startTime();
    }
    @override
    void activate() {
        super.activate();
        if(image1 == null) {
            loadImage();
        }
    }
    Future<void> loadImage() async {
        image1 = await Utils.loadImageByAssets3("assets/wallper/wallper1.jpg");
        image2 = await Utils.loadImageByAssets3("assets/wallper/wallper2.jpg");
        image3 = await Utils.loadImageByAssets3("assets/wallper/wallper3.jpg");
        image4 = await Utils.loadImageByAssets3("assets/wallper/wallper4.jpg");
        if(!mounted)return;
        setState((){});
    }
    void startTime() {
        if(timer == null) {
            const duration = Duration(seconds: 1);
            timer = Timer.periodic(duration, (timer) {
                if(timeCount - 1 > 0) {
                    setState(() {
                        timeCount --;
                    });
                } else {
                    if(currentContext != null && mounted) {
                       // Navigator.of(currentContext!).pushReplacementNamed("/home");
                    }
                    clearTimer();
                }
            });
        }
    }
    void clearTimer() {
        if(timer != null) {
            isTick = false;
            timer!.cancel();
            timer = null;
        }
    }
    @override
    void dispose() {
        clearTimer();
        super.dispose();
    }
}